package com.niiwoo.civet.trade.service.local.newRefund.notify.dubbo;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.activity.stage.dto.request.prize.UseInterestDiscountReqDTO;
import com.niiwoo.activity.stage.dto.response.prize.UserPrizeResponseDTO;
import com.niiwoo.activity.stage.enums.prize.UserPrizeTypeIdEnum;
import com.niiwoo.activity.stage.enums.prize.UserPrizeUseSceneEnum;
import com.niiwoo.activity.stage.service.prize.ActivityPrizeDubboService;
import com.niiwoo.civet.base.enums.RefundTypeEnum;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.RefundNotifyRecord;
import com.niiwoo.civet.trade.dao.entity.RefundOrder;
import com.niiwoo.civet.trade.service.local.newRefund.notify.RefundNotifyHandle;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyHandleResult;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;

import lombok.extern.slf4j.Slf4j;

/**
 * 活动中心-借款利率折扣事件
 * 
 * 触发条件：还款
 *
 * @author: kevin.jia
 * @version 1.0.0
 */
@Slf4j
@Component
public class ActivityPrizeInterestDiscountNotify implements RefundNotifyHandle {
	
	@Reference(version = "1.0.0")
	private ActivityPrizeDubboService activityPrizeDubboService;

	@Override
	public RefundNotifyHandleResult handle(RefundNotifyDTO refundNotifyDTO) {
		RefundNotifyHandleResult refundNotifyHandleResult = null;
		try {
			UseInterestDiscountReqDTO useInterestDiscountReqDTO = (UseInterestDiscountReqDTO) bulidParam(refundNotifyDTO);
			
			// 参数为空直接返回成功
			if (null == useInterestDiscountReqDTO) return new RefundNotifyHandleResult();
			
			log.info("调用活动中心-借款利率折扣事件，ProjectId：{}, 请求参数：{}", refundNotifyDTO.getProject().getProjectId(), JSONObject.toJSONString(useInterestDiscountReqDTO));
			activityPrizeDubboService.useInterestDiscountPrize(useInterestDiscountReqDTO);
			log.info("调用活动中心-借款利率折扣事件完成， ProjectId：{}", refundNotifyDTO.getProject().getProjectId());
			refundNotifyHandleResult = new RefundNotifyHandleResult();
		} catch (Exception e) {
			refundNotifyHandleResult = new RefundNotifyHandleResult(e.getMessage());
			log.error("调用活动中心-借款利率折扣事件异常，异常原因：{}, 请求参数：{}", e.getMessage(), JSONObject.toJSONString(refundNotifyDTO.getRefundNotifyRecord()), e);
		}
		return refundNotifyHandleResult;
	}

	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		Project project = refundNotifyDTO.getProject();
		RefundOrder refundOrder = refundNotifyDTO.getRefundOrder();
		RefundNotifyRecord refundNotifyRecord = refundNotifyDTO.getRefundNotifyRecord();
		
		log.info("调用活动中心-借款利率折扣事，请求参数:UserId:{}, PrizeId:{}", project.getBorrowUserId(), project.getPrizeId());
		UserPrizeResponseDTO userPrizeResponseDTO = activityPrizeDubboService.getUserPrizeInfo(project.getBorrowUserId(), project.getPrizeId());
        if (userPrizeResponseDTO == null) {
        	log.info("调用活动中心-借款利率折扣事，没有查询到相关红包，UserId:{}, PrizeId:{}", project.getBorrowUserId(), project.getPrizeId());
        	return null;
        }
        log.info("调用活动中心-借款利率折扣事，返回结果:{}", JSONObject.toJSONString(userPrizeResponseDTO));
        
        // 较验红包类型，非借款利息折扣红包直接返回
		if (!UserPrizeTypeIdEnum.BORROW_INTEREST_RATE_DISCOUNT.getPrizeType().equals(userPrizeResponseDTO.getPrizeTypeId())) {
			return null;
		}
		
		// 构建请求参数
		UseInterestDiscountReqDTO useInterestDiscountReqDTO = new UseInterestDiscountReqDTO();
		useInterestDiscountReqDTO.setUserId(project.getBorrowUserId());
		useInterestDiscountReqDTO.setPrizeId(project.getPrizeId());
		useInterestDiscountReqDTO.setCurrentPeriods(refundNotifyRecord.getCurrentPeriod());
		if (RefundTypeEnum.OVERDEPT.getRefundType().equals(refundOrder.getRefundType())) {
			useInterestDiscountReqDTO.setUserPrizeUseSceneEnum(UserPrizeUseSceneEnum.REPAYMENT_ADVANCE);
		}
		return useInterestDiscountReqDTO;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.ACTIVITY_PRIZE_INTEREST_DISCOUNT;
	}	
}